﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ORM.Business.Feedbacks;
using ORM.Business.Interfaces;
using ORM.Business.Models.Entities;
using ORM.Common.Logging;
using ORM.Resources;

namespace ORM.Business.Repositories
{
    public class GuideTypeRepository : IGuideTypeRepository
    {

        //-------------------------------------------------------------------------------------------------
        public Feedback<IList<GuideType>> GetAllGuideType()
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    var GuideType = entities.GuideTypes.Where(gt => !gt.IsDeleted).OrderBy(gt => gt.Name).ToList();
                    return new Feedback<IList<GuideType>>(true, null, GuideType);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<IList<GuideType>>(false, Messages.GeneralError);
            }
        }

        public Feedback DeleteGuideTypeCategory(int id)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    foreach (var guide in entities.Guides.Where(i => i.GuideTypeId == id).ToList())
                    {
                        guide.IsDeleted = true;
                    }
                    GuideType c = entities.GuideTypes.Single(i => i.Id == id);
                    c.IsDeleted = true;
                    entities.SaveChanges();

                    return new Feedback(true);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback(false, Messages.GeneralError);
            }
        }

        public Feedback<GuideType> InsertGuideType(GuideType t)
        {
            GuideType tnew = new GuideType
            {
                Name = t.Name,
                Description = t.Description,
                IsDeleted = false
            };

            try
            {
                using (var entities = new OrmEntities())
                {
                    //Check for duplicate name
                    var exist = entities.GuideTypes
                        .FirstOrDefault(i => i.Name == tnew.Name && !i.IsDeleted);
                    if (exist != null)
                        return new Feedback<GuideType>(false, Messages.InsertCategory_DuplicateName);

                    entities.GuideTypes.Add(tnew);

                    entities.SaveChanges();
                }
                return new Feedback<GuideType>(true, "Thêm loại hướng dẫn thành công", tnew);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<GuideType>(false, Messages.GeneralError);
            }
        }

        public Feedback<GuideType> UpdateGuideTypeCategory(GuideType c)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    // Check for duplicate name
                    GuideType exist = entities.GuideTypes.FirstOrDefault(i => i.Id != c.Id && i.Name == c.Name && !i.IsDeleted);
                    if (exist != null)
                        return new Feedback<GuideType>(false, Messages.InsertCategory_DuplicateName);

                    var category = entities.GuideTypes.Single(i => i.Id == c.Id);
                    category.Name = c.Name;
                    category.Description = c.Description;
                    entities.SaveChanges();

                    return new Feedback<GuideType>(true, null, category);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<GuideType>(false, Messages.GeneralError);
            }
        }

        public Feedback<GuideType> GetGuideTypeCategoryDetails(int id)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    GuideType category = entities.GuideTypes.SingleOrDefault(i => i.Id == id && !i.IsDeleted);
                    if (category != null)
                        return new Feedback<GuideType>(true, null, category);
                    else
                        return new Feedback<GuideType>(false, Messages.GetCategory_NotFound);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<GuideType>(false, Messages.GeneralError);
            }
        }

    }
}